package com.plant.tool;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
public class Testt {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        List<Map<String, String>> list = new ArrayList<Map<String,String>>();
        Map<String,String> map1 = new HashMap<String,String>();
        map1.put("count", "1");
        map1.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
        map1.put("province", "江苏省");
        map1.put("date", "2014-09-23 10:13:39");
        map1.put("channel", "东环大润发");
        map1.put("city", "苏州市");
        list.add(map1);
         
        Map<String,String> map2 = new HashMap<String,String>();
        map2.put("count", "1");
        map2.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
        map2.put("province", "江苏省");
        map2.put("date", "2014-09-23 10:13:39");
        map2.put("channel", "东环大润发");
        map2.put("city", "泰州市");
        list.add(map2);
         
        Map<String,String> map3 = new HashMap<String,String>();
        map3.put("count", "1");
        map3.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
        map3.put("province", "江苏省");
        map3.put("date", "2014-09-23 10:13:39");
        map3.put("channel", "东环大润发");
        map3.put("city", "泰州市");
        list.add(map3);
         
         
        Map<String,String> map4 = new HashMap<String,String>();
        map4.put("count", "1");
        map4.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
        map4.put("province", "江苏省");
        map4.put("date", "2014-09-23 10:13:39");
        map4.put("channel", "东环大润发");
        map4.put("city", "苏州市");
        list.add(map4);
         
        System.out.println(mergeList(list));
         
         
    }
 
     
    /**
     * 合并相同的Map
     * @param list
     * @return
     */
    public static List<Map<String,String>> mergeList(List<Map<String,String>> list){
        List<Map<String,String>> retList = new ArrayList<Map<String,String>>();
        for(int i=0;i<list.size();i++){
            Map<String,String> map = list.get(i);
            String name = map.get("name");
            String city = map.get("city");
            String count = map.get("count");
            Map<String,String> newMap=isExistSame(i,name,city,count,list);
            if(null==newMap){
                retList.add(map);
            }
            else{
                list.remove(map.get(i));
                retList.add(newMap);
            }
        }
        return retList;
    }
     
    private static Map<String,String> isExistSame(int i,String name,String city,String count,List<Map<String,String>> list){
        Map<String,String> newMap = null;
        for(int j=i+1;j<list.size();j++){
            Map<String,String> innerMap = list.get(j);
            String name1 = innerMap.get("name");
            String city1 = innerMap.get("city");
            String count1 = innerMap.get("count");
            if(name.equals(name1)&&city.equals(city1)){
                newMap = new HashMap<String,String>();
                newMap.put("count", ""+(Integer.parseInt(count)+Integer.parseInt(count1)));
                Iterator<String> keyIterator = innerMap.keySet().iterator();
                while(keyIterator.hasNext()){
                    String key = keyIterator.next();
                    if(!key.equals("count")){
                        newMap.put(key, innerMap.get(key));
                    }
                }
                list.remove(j);
            }
        }
        return newMap;
    }
}